from api.pts import PTS
from api.ps import PS

from studio.models import Studio,AppModel
from studio.Scanner import getAppRes
from studio.resource import get_app_resource
from studio.app_info import AppInfoBuilder
from common.db_tools import to_dict

class StudioCtx(object):
    def __init__(self,name):
        self.name=name
        self.title=name
        self.logo=''
        self.temp=''
        self.slogan=''
        self.dsp=''
        self.fea_app=''
        self.email=''
        self.policy=''
        self.copyright=''
        
        studio = Studio.objects.get(name=name)
        self._studio=studio
        self.__dict__.update(studio.__dict__)        
        
    def get_dict(self):
        self.build()
        return self.__dict__
        
    def build(self):
        self.get_fea_app()
        self.get_temp()
        
    def get_fea_app(self):
        fea_app_str = self.fea_app.rstrip(';')
        self.fea_app=[]
        if not fea_app_str:
            # if not select fea_app, then auto choice the dominant orientation and no more than 3 app as fea app
            pc= self._studio.appmodel_set.filter(orientation='portait',status__in=['normal','warning']).count()
            lc= self._studio.appmodel_set.filter(orientation='landscape',status__in=['normal','warning']).count()
            orient= 'portait' if pc > lc else 'landscape'
            count=min(3,max(pc,lc))
            for app in self._studio.appmodel_set.filter(orientation=orient,status__in=['normal','warning']).order_by('-date_released')[:count]:
                self.fea_app.append(app)
        else:
            skus = fea_app_str.split(';')
            for app in AppModel.objects.filter(sku__in=skus,status__in=['normal','warning']):
                self.fea_app.append(app)
            
            for orient in ['portait','landscape']:
                count= len([app for app in self.fea_app if app.orientation==orient])
                if count>0 and count%3 !=0:
                    for app in AppModel.objects.filter(studio=self._studio, orientation=orient,status__in=['normal','warning'])\
                    .exclude(sku__in=skus).order_by('-date_released')[:(3-count%3)]:
                        self.fea_app.append(app)
                
        for app in self.fea_app:
            app.screenshots = app.screenshots.rstrip(';')
            app.screenshots = app.screenshots.split(';')            

    def get_temp(self):
        if self._studio.temp:
            self.temp=self._studio.temp.name


class AppCtx(object):
    def __init__(self,name):
        self.name=name
        
        self.icon=''
        self.gp_link=''
        self.long_dsp='jj yy '
        self.screenshots=[]
        self.email='XXX@SSS.COM'
    
    def get_dict(self):
        # app = AppModel.objects.get(name=self.name)

        # app.screenshots = app.screenshots.rstrip(';')
        # app.screenshots = app.screenshots.split(';')
        self.build()
        return   {'app':self}     
    
    def build(self):
        app = AppModel.objects.get(name=self.name)
        
        app.screenshots = app.screenshots.rstrip(';')
        app.screenshots = app.screenshots.split(';')   
        
        self.title=self.name.replace('-',' ')+'-'+app.studio.name
        
        self.__dict__.update(app.__dict__)
        # app = AppModel.objects.get(name=self.name)
        # return to_dict(app)
        # app=AppInfoBuilder(self.sku)
        # app.update_info()
        # app.update_meta()
        # app.update_resource()
        # self.__dict__.update(app.__dict__)


class CollectionCtx(object):
    def __init__(self,studio_name):
        self.studio_name=studio_name
        self.apps=[]
    
    def get_dict(self):
        self.build()
        return self.__dict__
    
    def build(self):
        studio= Studio.objects.get(name=self.studio_name)
        self.apps=studio.appmodel_set.filter(status__in=['normal','warning'])
        # pts=PTS()
        # self.apps=AppInfoBuilder.get_apps(self.studio_name)
        # self.apps=filter(lambda x:x.is_valid(),self.apps)
        # for app in self.apps:
            # app.update_resource()
          
            
        
        